var loanId = $("#loan").data("id");

$(function () {
    
    var loadOperations = function () {
        // 初始化列表
        var table = $('#operationTable').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "operation/listByObject",
                "data": function(d) {
                    d.realm = "LOAN";
                    d.id = loanId;
                },
                "type": "POST"
            },
            "columns": [
                {"data": "employee"},
                {"data": "operation"},
                {"data": "operation"},
                {"data": "operation"}
            ],
            "aoColumnDefs": [
                {
                    "targets": 0, // 员工姓名
                    "mRender": function (data, type, row) {
                        return '<a href="employee/index/' + data.id + '">' + data.name + '</a>';
                    }
                }, {
                    "targets": 1, // 操作类型
                    "mRender": function (data, type, row) {
                        if (data.type in opTypeMap) {
                            return opTypeMap[data.type];
                        } else {
                            return "";
                        }
                    }
                }, {
                    "targets": 2, // 操作时间
                    "mRender": function (data, type, row) {
                        return $.format.date(new Date(data.recordTime), "yyyy-MM-dd HH:mm:ss");
                    }
                }, {
                    "targets": 3, // 备注
                    "mRender": function (data, type, row) {
                        return data.description;
                    }
                }
            ],
            "order": [2, 'desc'],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ",",
            },
            "iDisplayLength": 25,
            "aLengthMenu": [
                [5, 10, 25, 50, 100, -1],
                [5, 10, 25, 50, 100, "全部"]
            ],
            "sDom": '<"dt-panelmenu clearfix"l<"pull-right"Tf>r>t<"dt-panelfooter clearfix"ip>',
            "oTableTools": {
                "aButtons": [{
                        "sExtends": "xls",
                        "sButtonText": "导出为Excel"
                    }
                ],
                "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
            },
            "initComplete": function (settings, json) {
            }
        });
    };

    var initPage = function () {
        // 更新tab状态
        $("#tab-operations").removeClass("btn-default").addClass("btn-primary");
        loadOperations();
    };

    // 初始化页面
    initPage();
});
